<?php
namespace app\models\config;
use yii\base\Model;
use Yii;
use app\components\Fun;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserForm
 *
 * @author shuai
 */
class UserForm  extends Model{
          /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
          ['verifyCode', 'captcha','captchaAction'=>'/admin/site/captcha'],
            [['username','password_old','password','password2'], 'safe'], 
         /// ['verifyCode', 'captcha'],
           
        ];
    }
    
    public function attributeLabels()
	{
		
		return [ 
                     'username'=>'用户名',
                    'password_old'=>'旧密码',
                     'password'=>'密码',
                     'password2'=>'确认密码', 
                    'verifyCode' => '验证码',
		];
	}
        
        
        public $username;
        public $password_old;
        public $password;
        public $password2;
        public $verifyCode;
        
        public function save(){
            /*
            var_dump($this->validate());
            var_dump($this->getErrors());
            
            exit;
            
            return true;*/
            if(!Yii::$app->user->identity->validatePassword($this->password_old)){
                $this->addError('password_old','原密码错误');
                return false;
            }
            if(Fun::strlen($this->password)<3||Fun::strlen($this->password)>15){
                $this->addError('password','密码必须大于3和小于15');
                return false;
            }
            
            if($this->password!==$this->password2){
                $this->addError('password','密码不一致');
                return false;
            }
            
            Yii::$app->user->identity->upPassword($this->password);
            
            
            return true;
        }
        
}
